﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using DailyDiet.Models;
using DailyDiet.Models.VO;
using DailyDiet.Services;
using DailyDiet.Services.Constant;
using DailyDiet.Services.Navigation;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

namespace DailyDiet.ViewModels
{
    
    /// <summary>
    /// 推荐页ViewModel
    /// </summary>
    public class RecViewModel : ViewModelBase {

        /******** 构造函数 ********/

        private IPreferenceStorage _preference;
        private IGetRecService _getRecService;
        private IContentNavigationService _navigationService;
        private List<RecDietViewObject> _recDietViewObjects;
        private bool _isNew = true;
        public RecViewModel(
            IPreferenceStorage preferenceStorage,
            IGetRecService getRecService,
            IContentNavigationService contentNavigationService)
        {
            _preference = preferenceStorage;
            _getRecService = getRecService;
            _navigationService = contentNavigationService;

            //用于测试  对RecDietViewObjects进行初始化
            RecDietViewObjects = new List<RecDietViewObject>();
        }

        //********绑定属性*******
        public List<RecDietViewObject> RecDietViewObjects {
            get => _recDietViewObjects;
            set =>
                Set(nameof(RecDietViewObjects), ref _recDietViewObjects, value);
        }

        //*********绑定方法**************
        private RelayCommand _appearingCommand;
        public RelayCommand AppearingCommand =>
            _appearingCommand ?? (_appearingCommand =
                new RelayCommand(async () => await AppearingCommandFunc()));
        public async Task AppearingCommandFunc() {
            if (!_isNew)
            {
                return;
            }

            _isNew = false;

            List<RecDietViewObject> list = new List<RecDietViewObject>();
            list.Add(
                new RecDietViewObject() {
                    MealTimes = GetRecServiceConstant.BREAKFAST,
                    DietLists = await _getRecService.GetDietRecAsync(GetRecServiceConstant.BREAKFAST)
                });
            list.Add(
                new RecDietViewObject()
                {
                    MealTimes = GetRecServiceConstant.LUNCH,
                    DietLists = await _getRecService.GetDietRecAsync(GetRecServiceConstant.LUNCH)
                });
            list.Add(
                new RecDietViewObject()
                {
                    MealTimes = GetRecServiceConstant.DINNER,
                    DietLists = await _getRecService.GetDietRecAsync(GetRecServiceConstant.DINNER)
                });
            RecDietViewObjects = list;

        }


        /// <summary>
        /// 跳转至反馈页面
        /// </summary>
        private RelayCommand _goToFeedbackCommand;

        public RelayCommand GoToFeedbackCommand =>
            _goToFeedbackCommand ?? (_goToFeedbackCommand =
                new RelayCommand(async () => await GoToFeedbackCommandFunc()));

        public async Task GoToFeedbackCommandFunc()
        {
            await _navigationService.NavigateToAsync(ContentNavigationServiceConstants.DietFeedbackPage);
        }


        /// <summary>
        /// 处理点击事件命令
        /// </summary>
        private RelayCommand<RecDietViewObject> _recTappedCommand;
        public RelayCommand<RecDietViewObject> RecTappedCommand =>
            _recTappedCommand ?? (_recTappedCommand =
                new RelayCommand<RecDietViewObject>(async diets => await RecTappedCommandFunc(diets)));
        private async Task RecTappedCommandFunc(RecDietViewObject diets) =>
            await _navigationService.NavigateToAsync(ContentNavigationServiceConstants.RecDietsDetailPage, diets);

    }
}
